using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AntEater.Runtime.Services.Transformation;
using System.Xml.XPath;
using AntEater.ComponentModel.BuildElements;
using System.Xml;
using System.IO;

namespace AntEater.Runtime.Tests.Services.Transformation
{
    /// <summary>
    /// Summary description for BuildElementToXmlTransformationServiceTest
    /// </summary>
    [TestClass]
    public class BuildElementToXmlTransformationServiceTest
    {
        //public const string InputBuildFilePath = "..\\..\\..\\..\\UnitTests\\TestData\\BuildFiles\\AntEater.ComponentModel.csproj.xml";
        //public const string OutputBuildFilePath = "..\\..\\..\\..\\UnitTests\\TestData\\BuildFiles\\AntEater.ComponentModel_out.csproj.xml";
        public const string InputBuildFilePath = "..\\..\\..\\..\\UnitTests\\TestData\\BuildFiles\\SampleBuildFile.build.xml";
        public const string OutputBuildFilePath = "..\\..\\..\\..\\UnitTests\\TestData\\BuildFiles\\SampleBuildFile_out.build.xml";

        private BuildElementToXmlTransformationService _buildElementToXmlService;

        /// <summary>
        /// Gets the build element to XML service.
        /// </summary>
        /// <value>The build element to XML service.</value>
        private BuildElementToXmlTransformationService BuildElementToXmlService {
            get { return _buildElementToXmlService; }
        }

        private XmlToBuildElementTransformationService _xmlToBuildElementService;

        /// <summary>
        /// Gets the XML to build element service.
        /// </summary>
        /// <value>The XML to build element service.</value>
        private XmlToBuildElementTransformationService XmlToBuildElementService {
            get { return _xmlToBuildElementService; }
        }

        private Stream _inputBuildFile;

        /// <summary>
        /// Gets the input build file.
        /// </summary>
        /// <value>The input build file.</value>
        private Stream InputBuildFile {
            get { return _inputBuildFile; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:BuildElementToXmlTransformationServiceTest"/> class.
        /// </summary>
        public BuildElementToXmlTransformationServiceTest() {
            this._buildElementToXmlService = new BuildElementToXmlTransformationService();
            this._xmlToBuildElementService = new XmlToBuildElementTransformationService();
            this._inputBuildFile = File.OpenRead(InputBuildFilePath);
        }

        /// <summary>
        /// Transforms the build element to XML test.
        /// </summary>
        [TestMethod]
        public void TransformBuildElementToXmlTest() {
            XmlToBuildElementTransformationResult transformationResult
                = this.XmlToBuildElementService.Transform(this.InputBuildFile, InputBuildFilePath);

            Assert.IsNotNull(transformationResult.BuildFileDefinition.RootBuildElement.ChildElements);
            Assert.IsTrue(transformationResult.BuildFileDefinition.RootBuildElement.ChildElements.Count > 0);

            BuildElementToXmlTransformationResult result
                = this.BuildElementToXmlService.Transform(transformationResult.BuildFileDefinition,InputBuildFilePath);

            Assert.IsNotNull(result,"Result is null.");
            Assert.IsNotNull(result.BuildFile,"BuildFile is null.");
            Assert.IsNotNull(result.Errors,"Errors is null.");
            
            Stream outputBuildFile = File.Create(OutputBuildFilePath);

            Assert.IsTrue(result.Errors.Count == 0,"{0} Error(s) occured during Transformation!",result.Errors.Count);


            Utilities.CopyStream(result.BuildFile, outputBuildFile, 4096);

            outputBuildFile.Close();
        }
    }
}
